package com.sibbay.vta;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sibbay.tools.Sharable;
import com.sibbay.vta.data.Route;

public class ServiceListActivity extends CommonActivity {

	static final String EXTRA_ARRAY_ID = "ArrayId";
	static final String EXTRA_TITLE = "Title";

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.service_list);
        
        final Intent intent = getIntent();
        final Sharable sh = intent.getParcelableExtra(EXTRA_ARRAY_ID);
        final Object[][] arrayId = (Object[][]) sh.obj();
        final String titleStr = intent.getStringExtra(EXTRA_TITLE);
        
        final TextView title = (TextView) findViewById(R.id.title);
        title.setText(titleStr);
        
        final ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(new ArrListAdapter(this, arrayId));
        list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
		        final Intent intent = new Intent();
		        intent.setClassName(getPackageName(), DayServiceListActivity.class.getName());
		        intent.putExtra(DayServiceListActivity.EXTRA_ARRAY_ID, new Sharable(((ArrListAdapter)l.getAdapter()).getArrayId(pos)));
		        intent.putExtra(DayServiceListActivity.EXTRA_TITLE, ((TextView)v).getText());
		        intent.putExtra(DayServiceListActivity.EXTRA_MAP_URL, ((ArrListAdapter)l.getAdapter()).getMapUrl(pos));
		        startActivity(intent);
//		        overridePendingTransition(R.anim.push_left_out, R.anim.push_right_in);
			}
		});
    }
	
    private static final class ArrListAdapter extends BaseAdapter {
        
		private final Context mContext;
		private final Object[][] mArrayId;

        public ArrListAdapter(final Context context, final Object[][] arrayId) {
        	mContext = context;
        	mArrayId = arrayId;
        }

        @Override
        public int getCount() {
            return mArrayId.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final TextView tv;
            if (convertView == null) {
                tv = (TextView) LayoutInflater.from(mContext).inflate(
                        android.R.layout.simple_list_item_1, parent, false);
            } else {
                tv = (TextView) convertView;
            }
            tv.setText((String)mArrayId[position][Route.DATA_INFO]);
            return tv;
        }
        
        ///
        
        Object[][] getArrayId(int position) {
        	return (Object[][]) mArrayId[position][Route.DATA_DATA];
        }

        String getMapUrl(int position) {
        	return (String) mArrayId[position][Route.DATA_MAP];
        }
    }
}
